const jwt = require('jsonwebtoken')
const {secret} = require('../config/config')
const checkTokenMiddleware = function(req,res,next){
  let token = req.get('token')
  if (!token) {
    res.json({
      code: '2003',
      msg: 'token缺失',
      data: null
    })
    return
  }
  jwt.verify(token, secret,(err,data)=>{
    if(err){
      res.json({
        code:"2004",
        msg:'token验证不成功',
        data:null
      })
      return
    }
    req.user= data
    next()
  })
}

module.exports = checkTokenMiddleware